package mr

//
// RPC definitions.
//
// remember to capitalize all names.
//

import (
	"log"
	"net/rpc"
	"strconv"
)

//
// example to show how to declare the arguments
// and reply for an RPC.
//

type MapArgs struct {
	Filenames []string
}

type MapReply struct {
	Buckets map[string]string
}

type ReduceArgs struct {
	KeysFiles map[string][]string
}

type ReduceReply struct {
	N []string
}

// Add your RPC definitions here.

// Cook up a unique-ish UNIX-domain socket name
// in /var/tmp, for the coordinator.
// Can't use the current directory since
// Athena AFS doesn't support UNIX-domain sockets.
// we think -1 pid is coordinator
func getSockName(pid int) string {
	s := "/var/tmp/5840-mr-"
	s += strconv.Itoa(pid)
	return s
}

func rpcCall(pid int, rpcname string, args interface{}, reply interface{}) bool {
	sockname := getSockName(pid)
	c, err := rpc.DialHTTP("unix", sockname)
	if err != nil {
		log.Fatalln("dialing:", err)
	}
	defer c.Close()

	err = c.Call(rpcname, args, reply)
	if err == nil {
		return true
	}

	log.Println("RPC " + rpcname + " failed!")
	return false
}
